<?php

namespace plugins\logistics\controller;

use cmf\controller\PluginBaseController; //引用插件基类
use cmf\controller\PluginRestBaseController;
use think\Db;

class ApiIndexController extends PluginBaseController
{
    protected  $API;
    public function _initialize()
    {
        $dir =  dirname(__FILE__);
        $file=scandir($dir); 
        unset($file[0],$file[1]);//因为这样获取到的数组前两位是. 和..,所以删去
        foreach($file as $key =>$value)
        {
            $filename ='';
            if($value === 'ApiIndexController.php') continue;//跳出本次循环
            if(substr($value,0,3)=== 'Api')
            {
                $filename = $value; 
                break;//终止循环
            }
        }
        if(!empty($filename))
        {
            $filename = str_replace(strrchr($filename, "."),"",$filename);
            $res = '\\plugins\logistics\controller\\'.$filename; 
            
            $this->API = new $res();
        }
        // else{ echo 'false'; }
         
    }


    /**
     * 查询物流
     * @param  $isModule
     * @param  $ShipperCode   快递公司编码
     * @param  $LogisticCode  物流单号
     * @param  $store  店铺id
     * @return \返回json字符串
     */
    public function query($ShipperCode='',$LogisticCode='',$store='')
    {
        $order_num = $this->request->post('order_num');
        if(!empty($order_num)){
            $data = db('logistics_order')->where('order_num',$order_num)->find();
            if(!$data) return api_json_echo(false,'系统数据错误','',1000);
            $ShipperCode = $data['code'];
            $LogisticCode = $data['logistics_code'];
            $store = $data['store_id'];            
        }

        if(empty($LogisticCode)) return api_json_echo(true,'暂无物流信息');

        $res = $this->API->OrderTraces($ShipperCode,$LogisticCode,$store); 

        if(!empty($res['status'])) 
        {
            if($res['status'] == 'success'){
                $res = $res['data'];  
            }
            
        }else{
            $res = [];
        }
           
        return api_json_echo(true,'结果',$res,200);
    }



}